package manager.cms.service.exec;
import manager.cms.service.JdExecutor;
import manager.cms.service.utils.JdInternetWormUtils;
import manager.feign.GoodsListThirdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 获取搜索页面条件信息
 */
@Service(value = "searchGoodsExec")
public class SearchGoodsExecImpl implements JdExecutor {
    @Autowired
    private GoodsListThirdService goodsListThirdService;
    private Map<String,Object> resultMap;
    public SearchGoodsExecImpl(){
        this.resultMap=new HashMap<String,Object>();
    }
    public void execute(Map<String,Object> mapParams) {
        try {
                List<String> categoryIds=(List<String>)mapParams.get("categoryIds");
                String[] categoryStrIds = new String[categoryIds.size()];
                categoryIds.toArray(categoryStrIds);
                String  itemListHtml=goodsListThirdService.getGoodsList(categoryStrIds,1);
                String pattern = "\"J_selectorLine s-line\">[\\S\\s]*?</ul>";
                List<String> paramsStrList= JdInternetWormUtils.getStrArrayByReg(itemListHtml,pattern);
                List<Map<String,Object>> paramsMapList=new ArrayList<Map<String,Object>>();
                for (int i = 0; i < paramsStrList.size(); i++) {
                        String currStr=paramsStrList.get(i);
                        String keyPattern = "ng>[\\S\\s]*?:";
                        List<String> keyStrList= JdInternetWormUtils.getStrArrayByReg(currStr,keyPattern);
                        Map<String,Object> map=new HashMap<>();
                        String key=keyStrList.get(0);
                        map.put("key",key.substring(3,key.length()-1));
                        List<String> contentList=new ArrayList<>();
                        String contentPattern="</i>[\\S\\s]*?</a>";
                        List<String> paramsContentStrList= JdInternetWormUtils.getStrArrayByReg(currStr,contentPattern);
                        paramsContentStrList.forEach(item->{
                             contentList.add(item.substring(4,item.length()-4));
                        });
                        map.put("content",contentList);
                        paramsMapList.add(map);
                }
                resultMap.put("searchCondition",paramsMapList);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取执行的结果信息
     * @return
     */
    public Map<String,Object> getExecResult(){
          return resultMap;
    }
}
